﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using KFGame;


/// <summary>
///  键盘模拟操作数据，发送事件数据
/// </summary>
[DisallowMultipleComponent]
public class KeyboardSimulation : MonoBehaviour
{

    static List<IKFKeyBoardEvent> listenors = new List<IKFKeyBoardEvent>();
    public static void addListen(IKFKeyBoardEvent key)
    {
        listenors.Add(key);
    }
    public static void removeListen(IKFKeyBoardEvent key)
    {
        listenors.Remove(key);
    }

    List<KeyEventSimulation> lesionkeys = new List<KeyEventSimulation>();

    public delegate bool KeyDownEvent(KeyEventSimulation key);
    public delegate bool KeyUPEvent(KeyEventSimulation key);
    public delegate bool KEyEvent(KeyEventSimulation key);

    public KeyDownEvent onKeyDownEvent;
    public KeyUPEvent onKeyUpEvent;

    /// <summary>
    /// 按钮事件发出
    /// </summary>
    public KEyEvent onKeyEvent;

    public static string path = "KFConfig/kf_keys";
    static string iniPath = "/KFConfig/kfkeyslisent";


    /// <summary>
    /// 加载配置中，框架的 对键盘的侦听事件
    /// </summary>
    public void loadKFKey()
    {
        TextAsset ta = Resources.Load(path) as TextAsset;
        INIParser iNIParser = new INIParser();
        if (ta)
            iNIParser.Open(ta);

        loadConfig(iNIParser);
    }

    /// <summary>
    /// 加载配置中，对键盘的侦听事件
    /// </summary>
    public void load()
    {
        INIParser iNIParser = iNIParser = KFFile.InitParser(iniPath);
        loadConfig(iNIParser);
    }

    /// <summary>
    /// 加载配置中，对键盘的侦听事件
    /// </summary>
    void loadConfig(INIParser iNIParser)
    {

        List<string> groups = iNIParser.SectionNames();
        foreach (var item in groups)
        {
            KeyEventSimulation template = new KeyEventSimulation();
            template.name = iNIParser.ReadValue(item, "name", "KeyDefault");
            template.id = iNIParser.ReadValue(item, "id", 0);
            template.eventName = iNIParser.ReadValue(item, "eventName", "KFE_event0");
            template.type = iNIParser.ReadValue(item, "targettype", "KFE_TYPE_KFEVENT_GAME");
            template.keyCode = iNIParser.ReadValue(item, "keycode", "");
            template.valueInt = iNIParser.ReadValue(item, "valueInt", 0);
            template.valueString = iNIParser.ReadValue(item, "valueString", "");
            template.valufloat = (float)iNIParser.ReadValue(item, "valufloat", 0.0);
            template.value = iNIParser.ReadValue(item, "value", "");
            template.keyEventType = (KeyEventSimulation.KeyEventType)iNIParser.ReadValue(item, "keytype", 4);
            addLisent(template);
        }

    }




    /// <summary>
    /// add new lisent event for key
    /// </summary>
    /// <param name="keys"></param>
    public void addLisent(KeyEventSimulation keys)
    {
        lesionkeys.Add(keys);
    }

    /// <summary>
    /// clear all key lisents 
    /// </summary>
    public void clear()
    {
        lesionkeys.Clear();
    }

    void toEvent(KeyEventSimulation key)
    {
        KF.kf.kfevents.Pop(key.GetEvent());
    }


    /// <summary>
    /// Update is called once per frame
    /// </summary>
    void Update()
    {

        foreach (KeyEventSimulation e in lesionkeys)
        {

            switch (e.keyEventType)
            {
                //按钮
                case KeyEventSimulation.KeyEventType.Button:
                    if (Input.GetKeyUp(e.keyCode))
                    {
                        e.status = 1;
                        if (onKeyDownEvent != null)
                            onKeyDownEvent(e);
                        if (onKeyEvent != null)
                            onKeyEvent(e);
                         Debug.Log("keyboard Simulation key down btn:" + e.keyCode);
                        toEvent(e);

                    }
                    break;
                //弹起
                case KeyEventSimulation.KeyEventType.Up:
                    if (Input.GetKeyUp(e.keyCode))
                    {
                        e.status = 1;
                        if (onKeyDownEvent != null)
                            onKeyDownEvent(e);
                        if (onKeyEvent != null)
                            onKeyEvent(e);

                       Debug.Log("keyboard Simulation key up btn:" + e.keyCode);
                        toEvent(e);

                    }
                    break;
                //按下
                case KeyEventSimulation.KeyEventType.Down:
                    if (Input.GetKeyDown(e.keyCode))
                    {
                        e.status = 0;
                        if (onKeyUpEvent != null)
                            onKeyUpEvent(e);
                        if (onKeyEvent != null)
                            onKeyEvent(e);

                        toEvent(e);
                    }
                    break;

                //轴的模拟
                case KeyEventSimulation.KeyEventType.Axis:
                    e.valufloat = Input.GetAxis(e.keyCode);
                    if (onKeyEvent != null)
                        onKeyEvent(e);
                    break;
            }

        }
    }
}
